home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / bmgrep.arc / SEARCH.C < prev   
Text File  |  1986-12-09  |  922b  |  38 lines

  1. #include "bm.h"
  2. #include "extern.h"
  3.  
  4. int Search(Pattern,PatLen,EndBuff, Skip1, Skip2, Desc)
  5. register char Pattern[];
  6. int PatLen;
  7. char *EndBuff;
  8. unsigned short int Skip1[], Skip2[];
  9. struct PattDesc *Desc;
  10. {
  11.     register char *k, /* indexes text */
  12.         *j; /* indexes Pattern */
  13.     register int Skip; /* skip distance */
  14.     char *PatEnd,
  15.     *BuffEnd; /* pointers to last char in Pattern and Buffer */
  16.     BuffEnd = EndBuff;
  17.     PatEnd = Pattern + PatLen - 1;
  18.  
  19.     k = Desc->Start;
  20.     Skip = PatLen-1;
  21.     while ( Skip <= (BuffEnd - k) ) {
  22.         j = PatEnd;
  23.         k = k + Skip;
  24.         while (*j == *k) {
  25.             if (j == Pattern) {
  26.                 /* found it. Start next search
  27.                 * just after the pattern */
  28.                 Desc -> Start = k + Desc->PatLen;
  29.                 return(1);
  30.             } /* if */
  31.             --j; --k;
  32.         } /* while */
  33.         Skip = max(Skip1[*(unsigned char *)k],Skip2[j-Pattern]);
  34.     } /* while */
  35.     Desc->Start = k+Skip-(PatLen-1);
  36.     return(0);
  37. } /* Search */
  38.